package com.example.bighomework.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.bighomework.R;
import com.example.bighomework.adapter.NewsListAdapter;
import com.example.bighomework.bean.juheData;
import com.example.bighomework.bean.NewsInfo;
import com.google.gson.Gson;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class NewsFragment extends Fragment {

    private String URL = "http://v.juhe.cn/toutiao/index?key="+juheData.getKey()+"&type=tiyu";
    private RecyclerView recyclerView;
    private NewsListAdapter newsListAdapter;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == 100) {
                String data = (String)msg.obj;
                NewsInfo newsInfo = new Gson().fromJson(data,NewsInfo.class);
//刷新适配器
            if(null!=newsListAdapter) {
                if (newsInfo.getError_code()==10012){
                    return;
                }
                newsListAdapter.setListData(newsInfo.getResult().getData());
            }
           }
        }
     };

    @Override
    public View onCreateView(final LayoutInflater inflater,
                             ViewGroup container,Bundle savedInstanceState){
    final View view = inflater.inflate(R.layout.fragment_news,
            container,false);
    recyclerView=view.findViewById(R.id.newsList);
    newsListAdapter = new NewsListAdapter(getContext());
    recyclerView.setAdapter(newsListAdapter);
    getHttpData();
    return view;
    }
    private void getHttpData(){
//创建OkHttpClient对象
    OkHttpClient okHttpClient = new OkHttpClient();
//构造Request对象
    Request request = new Request.Builder()
        .url(URL)
        .get()
        .build();
//通过OkHttpClient和Request对象来构建call对象
    Call call = okHttpClient.newCall(request);
//通过call对象的enqueue（callback）方法来执行异步请求
    call.enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            Log.d("------------","onFailure:"+e.toString());
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws
                IOException {
// Log.d("------------","onResponse:"+request.body().string());
            String data = response.body().string();
            Message message = new Message();
//指定一个标识符
            message.what = 100;
            message.obj = data;
            mHandler.sendMessage(message);
        }
    });
    }
}